package newxml;

import com.jme.app.SimpleGame;
import com.jme.scene.Spatial;
import com.jme.util.resource.ResourceLocator;
import com.jme.util.resource.ResourceLocatorTool;
import com.jme.util.resource.SimpleResourceLocator;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;

public class LoadXMLScene extends SimpleGame {

    private File file = new File(".");

    public LoadXMLScene() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {/*nevermind*/}

        JFileChooser jfc = new JFileChooser(file);
        jfc.setFileFilter(new FileFilter() {

            public boolean accept(File f) {
                return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml");
            }

            public String getDescription() {
                return "XML files";
            }
            });
        if (jfc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
            System.exit(0);
        }
        file = jfc.getSelectedFile();
    }

    @Override
    protected void simpleInitGame() {
        try {
            ResourceLocator rl = new SimpleResourceLocator(file.getParentFile().toURI().toURL());
            ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, rl);
            Spatial spat = (Spatial) new XMLImporter().load(file);
            ResourceLocatorTool.removeResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, rl);
            rootNode.attachChild(spat);
        } catch (URISyntaxException ex) {
            Logger.getLogger(LoadXMLScene.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(LoadXMLScene.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {
        new LoadXMLScene().start();
    }
}
